﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Reflection;
using Rocket.Core.Common;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Rocket.Core
{
    [Serializable]
    [HasSelfValidation]
    public abstract class BaseObject : IObject
    {
        private AdditionalData _data;
        private int _id;
        private object _sourcePoco;
        [Browsable(false)]
        [NotNullValidator]
        public object SourcePoco
        {
            get { return _sourcePoco; }
            set { _sourcePoco = value; }
        }

        [Browsable(false)]
        public AdditionalData Data
        {
            get { return _data; }
            set { _data = value; }
        }

        [Browsable(false)]
        public bool IsNew
        {
            get;
            set;
        }

        [Browsable(false)]
        public int Id
        {
            get { return _id; }
            set 
            {
                _id = value;
                if (_id == 0)
                    IsNew = true;
            }
        }

        public virtual bool FoundIt(string query)
        {
            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                object x = pi.GetValue(this, null);
                if (x == null) continue;
                string s = x.ToString().ToLower();
                string d = query.Trim().ToLower();
                if (s.IndexOf(d) != -1) return true;
            }
            return false;
        }

        public virtual void DoValidate(ValidationResults results)
        { }
    }
}
